package com.wn.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.ProposalDao;
import com.wn.entity.PageParam;
import com.wn.entity.Proposal;
import com.wn.service.ProposalService;
import com.wn.utils.Rsp;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (Proposal)表服务实现类
 *
 * @author makejava
 * @since 2020-11-03 12:58:09
 */
@Service("proposalService")
@Transactional(rollbackFor = Exception.class)
public class ProposalServiceImpl implements ProposalService {
    @Resource
    private ProposalDao proposalDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Proposal queryById(String id) {
        return this.proposalDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param proposal 实例对象
     * @return 实例对象
     */
    @Override
    public Integer insert(Proposal proposal) {
        proposal.setId(UUID.randomUUID().toString().replaceAll("-",""));
        return this.proposalDao.insert(proposal);
    }

    /**
     * 修改数据
     *
     * @param proposal 实例对象
     * @return 实例对象
     */
    @Override
    public Integer update(Proposal proposal) {

        return this.proposalDao.update(proposal);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.proposalDao.deleteById(id) > 0;
    }

    @Override
    public Rsp updateId(Proposal proposal) {
        return proposalDao.update(proposal)==1?Rsp.success("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp deleteOneTrue(String id) {
        return proposalDao.deleteOneTrue(id)==1 ? Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }

    @Override
    public PageInfo<Proposal> searchSomeByPage(PageParam<Proposal> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Proposal> newscategoryList=proposalDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Proposal> newscategoryPageInfo = new PageInfo<Proposal>(newscategoryList);

            return newscategoryPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Proposal> newscategoryList=proposalDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Proposal> newscategoryPageInfo = new PageInfo<Proposal>(newscategoryList);

            return newscategoryPageInfo;

        }
    }


}
